<style>
    .laytable-cell-checkbox .layui-disabled.layui-form-checked i {
        background: #fff !important;
    }
</style>
<div class="layui-fluid layui-anim website-anim" id="website-expert" lay-title="专家管理">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="expert-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">专家姓名</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" autocomplete="off" class="layui-input" placeholder="请输入专家姓名">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">学科分类</label>
                                        <div class="layui-input-inline">
                                            <select name="subjectTypeCode">
                                                <option value="">请选择</option>
                                                <option
                                                        th:each="item : ${subjectTypeList}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">发布状态</label>
                                        <div class="layui-input-inline">
                                            <select name="isPublish">
                                                <option value="">请选择</option>
                                                <option value="true">已发布</option>
                                                <option value="false">未发布</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">京内/京外专家</label>
                                        <div class="layui-input-inline">
                                            <select name="districtTypeCode">
                                                <option value="">请选择</option>
                                                <option
                                                        th:each="item : ${districtList}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">理论/实务专家
                                        </label>
                                        <div class="layui-input-inline">
                                            <select name="featureTypeCode">
                                                <option value="">请选择</option>
                                                <option
                                                        th:each="item : ${featureList}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm3 layui-col-xs3 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action" id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" id="reset">
                                    重置
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" shiro:hasAnyPermissions="expert:add" id="expertAdd">
                                    新增
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" shiro:hasAnyPermissions="expert:import,expert:list" id="importExcel">
                                    导入
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" shiro:hasAnyPermissions="expert:export" id="exportExcel">
                                    导出
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action" id="downloadTemplate">
                                    模板
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action" shiro:hasAnyPermissions="expert:update" id="publishAll">
                                    批量发布
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="expertTable" lay-data="{id: 'expertTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="expert-class">
    {{#
    var dict=layui.dict.getByCode(d.subjectTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="expert-district">
    {{#
    var dict=layui.dict.getByCode(d.districtTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="expert-feature">
    {{#
    var dict=layui.dict.getByCode(d.featureTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="expert-publish">
    <input type="checkbox" {{ d.isPublish ? 'checked' : '' }} value="{{d.expertId}}" lay-filter="publish_status" lay-skin="switch" lay-text="发布|取消"/>
</script>
<script type="text/html" id="exist-user">
    {{#  if(d.existUser){ }}
    <span> 是 </span>
    {{#  } else{ }}
    <span> 否 </span>
    {{#  } }}
</script>

<script type="text/html" id="expert-option">
    <span shiro:lacksPermission="expert:list,expert:update,expert:delete">
        <span class="layui-badge-dot website-bg-orange"></span> 无权限
    </span>
    {{# if(d.isPublish && !d.existUser){}}
    <a lay-event="user" shiro:hasPermission="expert:update" class="website-orange">分配</a>
    {{# } }}

    {{# if(d.isPublish){}}
    <a lay-event="detail" shiro:hasPermission="expert:list" class="website-green">详情</a>
    {{# } else { }}
    <a lay-event="detail" shiro:hasPermission="expert:list" class="website-green">详情</a>
    <a lay-event="edit" shiro:hasPermission="expert:update" class="website-blue">修改</a>
    <a lay-event="del" shiro:hasPermission="expert:delete" class="website-red">删除</a>
    {{# } }}
</script>
<script type="text/html" id="checkbd">
    {{#  if (!d.isPublish){ }}
    <input type="checkbox" name="siam_one" title="" lay-skin="primary" data-id = "{{ d.expertId }}">
    {{#  } }}
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'form', 'table', 'website','upload','dict'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            dict = layui.dict,
            $view = $('#website-expert'),
            $query = $view.find('#query'),
            $exportExcel = $view.find('#exportExcel'),
            $publishAll = $view.find('#publishAll'),
            $reset = $view.find('#reset'),
            $expertAdd = $view.find('#expertAdd'),
            $downloadTemplate = $view.find('#downloadTemplate'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            upload = layui.upload,
            tableIns,
            createTimeFrom,
            createTimeTo;

           upload.render({
            elem: "#importExcel",//导入id
            url: "expert/import",
            size: '3072',
            accept: "file",
            exts: 'xls|xlsx|xlsm|xlt|xltx|xltm',
            done: function (result) {
                if (result.code == 200) {
                    var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
                    tableIns.reload({where: params, page: {curr: 1}});
                    layer.msg("导入成功")
                }else {
                    var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
                    tableIns.reload({where: params, page: {curr: 1}});
                    layer.msg(result.message)
                }
            }
        });
        form.render();
        initTable();
        //多选
        form.on("checkbox(siam_all)", function () {
            var status = $(this).prop("checked");
            $.each($("input[name=siam_one]"), function (i, value) {
                $(this).prop("checked", status);
            });
            form.render();
        });
        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('expertTable');
                if (name === 'add') {
                    website.modal.open('新增专家', 'system/expert/add', {
                        btn: ['提交', '重置'],
                        area: $(window).width() <= 750 ? '95%' : '50%',
                        offset: '30px',
                        yes: function (index, layero) {
                            $('#expert-add').find('#submit').trigger('click');
                        },
                        btn2: function () {
                            $('#expert-add').find('#reset').trigger('click');
                            return false;
                        }
                    });
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        website.alert.warn('请选择需要删除的用户');
                    } else {
                        website.modal.confirm('删除用户', '确定删除该用户？', function () {
                            var userIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                userIds.push(item.expertId)
                            });
                            deleteExperts(userIds.join(','));
                        });
                    }
                }
            },
            options: [{
                name: 'add',
                title: '新增专家',
                perms: 'expert:add'
            }, {
                name: 'delete',
                title: '删除专家',
                perms: 'expert:delete'
            }]
        });
        /**
         * 发布按钮的点击事件
         */
        form.on('switch(publish_status)', function (obj) {
            website.post(ctx + 'expert/pubish/' + this.value, {isPublish: obj.elem.checked}, function () {
                var pulishMessage='发布成功';
                if(!obj.elem.checked){
                    pulishMessage='取消发布成功';
                }
                website.alert.success(pulishMessage);
                $query.click();
            });
        });
        table.on('tool(expertTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                website.modal.view('专家信息', 'system/expert/detail/' + data.expertId, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'del') {
                website.modal.confirm('删除专家', '确定删除该专家？', function () {
                    deleteExperts(data.expertId);
                });
            }
            if (layEvent === 'edit') {
                website.modal.open('修改专家', 'system/expert/update/' + data.expertId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#expert-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
            if (layEvent === 'user') {
                website.modal.open('分配专家', 'system/expert/user/' + data.expertId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#expert-user').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });
        table.on('sort(expertTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });
        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $exportExcel.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            website.download(ctx + 'expert/export', params, '专家导出.xlsx');
        });
        $publishAll.on('click', function () {
            //获取所有选中值
            var ids = [];
            $.each($("input[name=siam_one]:checked"), function (i, value) {
                ids[i] = $(this).attr("data-id");  // 如果需要获取其他的值 需要在模板中把值放到属性中 然后这里就可以拿到了
            });
            if(ids.length==0||ids==null){
                website.alert.error("请至少勾选一个专家");
                return false;
            }
            console.log(ids);
            pubishAll(ids.join(','));
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });
        $expertAdd.on('click', function () {
            website.modal.open('新增专家', 'system/expert/add', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                offset: '30px',
                yes: function (index, layero) {
                    $('#expert-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#expert-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });
        $downloadTemplate.on('click', function () {
            var params = null;
            website.download(ctx + 'expert/downloadT', params, '专家模板.xlsx');
        });
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'expertTable',
                url: ctx + 'expert/list',
                cols: [[
                  //多选
                  {templet: "#checkbd", title: "<input type='checkbox' name='siam_all' title='' lay-skin='primary' lay-filter='siam_all'>", width: 60},
                  /*  {templet: "#checkbd", title: "", width: 50},*/
                    { type: 'numbers', title: '序号'},
                    {field: 'name', title: '专家姓名', minWidth: 100},
                    {title: '学科分类', templet: '#expert-class'},
                    {field: 'company', title: '所在单位'},
                    {title: '京内/京外专家',align:'center', templet: '#expert-district'},
                    {title: '存在账号', templet: '#exist-user',minWidth: 100},
                    {title: '发布状态', templet: '#expert-publish',minWidth: 100},
                    {field: 'mobile', title: '移动手机', minWidth: 100},
                    {title: '理论/实务专家', align:'center', templet: '#expert-feature',minWidth: 150},
                    {title: '操作', align:'center', toolbar: '#expert-option', minWidth: 200}
                ]],
                done: function (res, curr, count) {
                    //修改未选中
                    $("input[name=siam_all]").prop("checked", false);
                    form.render();
                }
            });
        }
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            var createTime = params.time;
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            params.invalidate_ie_cache = new Date();
            params.createTimeFrom = createTimeFrom;
            params.createTimeTo = createTimeTo;
            return params;
        }
        function deleteExperts(expertIds) {
            website.get(ctx + 'expert/delete/' + expertIds, null, function () {
                website.alert.success('删除专家成功');
                $query.click();
            });
        }
        function pubishAll(expertIds) {
            website.post(ctx + 'expert/pubishAll/' + expertIds, null, function () {
                website.alert.success('发布专家成功');
                $query.click();
            });
        }
    })
</script>